
package com.kanjian.music.entity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.kanjian.music.KanjianApplication;

public class Feed extends BaseCode {
    
    public static final int FEEDTYPE_FOLLOW_MUSICIAN = 1;
    
    public static final int FEEDTYPE_LISTEN_MUSIC = 2;

    // 动态类型 1关注音乐人 2收听音乐
    @SerializedName("feed_type")
    public int mFeedType;

    @SerializedName("feed_name")
    public String mFeedName;

    @SerializedName("feed_time")
    public long mFeedTime;

    @SerializedName("mid")
    public long mMusicId;

    @SerializedName("music_name")
    public String mMusicName;

    @SerializedName("uid")
    public int mUserId;

    @SerializedName("user_name")
    public String mUserName;

    @SerializedName("user_location")
    public String mUserLocation;

    @SerializedName("genre")
    public int[] mGenre;

    @SerializedName("user_pic")
    public String mUserPic;
    
    @SerializedName("new_song")
    public Music mNewSong;
    
    @SerializedName("is_fav")
    public int mIsFav;
    
    @SerializedName("music_url")
    public String mMuiscUrl;
    
    @SerializedName("play_time")
    public int mPlayTime;

    public static ArrayList<Feed> getFeedListFromJson(String jsonStr) {
        JSONObject jobj = getJSONObjectFromString(jsonStr);
        if (jobj != null) {
            try {
                return KanjianApplication.GSON.fromJson(jobj.getJSONArray("feed_list").toString(),
                        new TypeToken<List<Feed>>() {
                        }.getType());
            } catch (JsonSyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }

    public String getActionString() {
        if (mFeedType == 1) {
            return mFeedName + "关注了音乐人" + mUserName;
        } else {
            return mFeedName + "收听了音乐: " + mMusicName;
        }
    }

    public String getTimeDifferent() {
        
        long diff = System.currentTimeMillis() - mFeedTime * 1000;
        long diffSeconds = diff / 1000 % 60;
        long diffMinutes = diff / (60 * 1000) % 60;
        long diffHours = diff / (60 * 60 * 1000) % 24;
        long diffDays = diff / (24 * 60 * 60 * 1000);
        if (diffDays > 0) {
            return diffDays + "天前";
        } else if (diffHours > 0) {
            return diffHours + "小时前";
        } else if (diffMinutes > 0) {
            return diffMinutes + "分钟前";
        } else {
            return diffSeconds + "秒前";
        }
    }

}
